import React from "react";

import BaseModel from "./BaseModel";
import Constants from "../common/Constants";
import ResponseConstants from "../common/ResponseConstants";
import ToastUtils from "../common/ToastUtils";

const FPS = 400; //ms
const OVER_TIME = 8000;

export default class BaseModelNew extends BaseModel {
  constructor(props) {
    super(props);

    this._sendPatchUrlSaveObject = {};
    this._sendPatchTimerSaveObject = {};
  }

  sendPost(
    url,
    data,
    callback,
    responseType = ResponseConstants.responseType_json,
    forceRequest = false
  ) {
    this.sendFetch(url, data, callback, responseType, "POST", forceRequest);
  }

  sendGet(
    url,
    callback,
    responseType = ResponseConstants.responseType_json,
    forceRequest = false
  ) {
    this.sendFetch(url, null, callback, responseType, "GET", forceRequest);
  }

  sendOptions(
    url,
    data,
    callback,
    responseType = ResponseConstants.responseType_json
  ) {
    this.sendFetch(url, data, callback, responseType, "OPTIONS");
  }

  sendPutNew(
    url,
    callback,
    responseType = ResponseConstants.responseType_json
  ) {
    this.sendFetch(url, null, callback, responseType, "PUT");
  }

  sendPutPostJson(
    url,
    data,
    callback,
    responseType = ResponseConstants.responseType_json
  ) {
    this.sendFetch(url, data, callback, responseType, "PUT");
  }

  sendDeleteNew(
    url,
    data,
    callback,
    responseType = ResponseConstants.responseType_json
  ) {
    this.sendFetch(url, data, callback, responseType, "DELETE");
  }

  sendPatchNew(
    url,
    data,
    callback,
    responseType = ResponseConstants.responseType_json
  ) {
    this.sendFetch(url, data, callback, responseType, "PATCH");
  }

  sendFetch(
    url,
    data,
    callback,
    responseType,
    requestType,
    forceRequest = false
  ) {
    //if(url.indexOf("num") < 0) console.log("sendFetch", url, data, requestType);
    if (
      forceRequest === false &&
      this._sendPatchTimerSaveObject[url + requestType] != null &&
      this._sendPatchTimerSaveObject[url + requestType] != undefined
    ) {
      //if(url.indexOf("num") < 0) console.log("Error:" + FPS + "毫秒内重复请求同一个url + requestType:" + url + " " + requestType);
      return;
    }
    let self = this;
    if (forceRequest === false) {
      this._sendPatchTimerSaveObject[
        url + requestType
      ] = setTimeout(function() {
        clearTimeout(self._sendPatchTimerSaveObject[url + requestType]);
        self._sendPatchTimerSaveObject[url + requestType] = null;
      }, FPS);
    }

    if (data && data instanceof Object) {
      data = JSON.stringify(data);
    }
    var cloneResponse = null;
    var success = false;
    var overtimeTimer = setTimeout(function() {
      clearTimeout(overtimeTimer);
      // ToastUtils.show("请求超时" + url);
    }, OVER_TIME);
    fetch(url, {
      method: requestType,
      mode: "cors",
      headers: this._getHeader(requestType),
      body: data
    })
      .then(function(response) {
        clearTimeout(overtimeTimer);
        //if(url.indexOf("num") < 0) console.log("sendFetch back response:", url, requestType, response);
        cloneResponse = response.clone();
        success = self._checkStatusIsSuccess(cloneResponse.status);
        if (responseType === ResponseConstants.responseType_json) {
          return response.json();
        } else {
          return response.text();
        }
      })
      .then(response => {
        //if(url.indexOf("num") < 0) console.log("sendFetch back json:", url, requestType, response);
        callback && callback(response, success, cloneResponse);
      })
      .catch(error => {
        console.log("sendFetch back error:", url);
        callback && callback(null, success, cloneResponse);
      });
  }

  //请求链接
  sendRequestFile(url, callback) {
    //console.log("sendRequestFile", url);
    let cloneResponse = null;
    var success = false;
    var self = this;
    fetch(url)
      .then(response => {
        //console.log("sendRequestFile back", response);
        cloneResponse = response.clone();
        success = self._checkStatusIsSuccess(cloneResponse.status);
        return response.text();
      })
      .then(responseData => {
        responseData = responseData.replace(/[\r\n]/g, "");
        callback(responseData, success, cloneResponse);
      })
      .catch(error => {
        console.log(error);
        callback(null, success, response);
      });
  }

  _checkStatusIsSuccess(status) {
    return (
      status === ResponseConstants.status_request_success ||
      status === ResponseConstants.status_created_success ||
      status === ResponseConstants.status_deal_success
    );
  }
}
